package com.wqiuqiu.gulimall.ware.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wqiuqiu.gulimall.ware.vo.MergePurchaseVo;
import com.wqiuqiu.gulimall.ware.vo.PurchaseDoneVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.wqiuqiu.gulimall.ware.entity.PurchaseEntity;
import com.wqiuqiu.gulimall.ware.service.PurchaseService;
import com.wqiuqiu.common.PageUtils;
import com.wqiuqiu.common.R;



/**
 * 采购信息
 *
 * @author wqiuqiu
 * @email 18773489795@163.com
 * @date 2023-11-29 12:16:31
 */
@RestController
@RequestMapping("ware/purchase")
public class PurchaseController {
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 查询未领取的采购单列表
     */
    @GetMapping("/unreceive/list")
    public R unreceiveList(@RequestParam Map<String, Object> params){
        PageUtils page = purchaseService.queryUnreceiveList(params);

        return R.ok().put("page", page);
    }
    /**
     * 查合并采购需求
     */
    @PostMapping("/merge")
    public R mergePurchaseList(@RequestBody MergePurchaseVo mergePurchaseVo){
        if (purchaseService.mergePurchaseList(mergePurchaseVo))
            return R.ok();
        else{
            //TODO 前端的页面提示还需完善
            return R.error(400,"采购单已经领取或者采购项正在采购，不能被合并分配");
        }
    }
    /**
     * 完成采购
     * id: 123,//采购单id
     *    items: [{itemId:1,status:4,reason:""}]//完成/失败的需求详情
     */
    @PostMapping("/done")
    public R donePurchase(@RequestBody PurchaseDoneVo purchaseDoneVo){
        purchaseService.donePurchase(purchaseDoneVo);
        return R.ok();
    }
    /**
     * 领取采购单
     */
    @PostMapping("/received")
    public R receivePurchaseList(@RequestBody List<Long> ids){
        purchaseService.receivePurchaseList(ids);
        return R.ok();
    }



    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		PurchaseEntity purchase = purchaseService.getById(id);

        return R.ok().put("purchase", purchase);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PurchaseEntity purchase){
        purchase.setCreateTime(new Date());
        purchase.setUpdateTime(new Date());
		purchaseService.save(purchase);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PurchaseEntity purchase){
		purchaseService.updateById(purchase);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		purchaseService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
